#include "inforecord.h"
#include <QDebug>
#include"dbutil.h"
using namespace std;
InfoRecord::InfoRecord(QAbstractTableModel *parent) : QAbstractTableModel(parent)
{
}

int InfoRecord::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return model_data.size();
}
int InfoRecord::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return 3;
}
void InfoRecord::getData(void){
    model_data.clear();
    Dbutil db;
    QSqlDatabase data= db.connect();

    QSqlQuery dataset;

    string sql_data;


    sql_data="select * from warninginfo ";
    const char *k_data = sql_data.c_str();
    bool success_data=dataset.exec(k_data);
    if(success_data){
        int count=1;
        while(dataset.next()){
            model_data<<InfoDate{count,
                        dataset.value(1).toString(),
                        dataset.value(2).toString(),
                        dataset.value(3).toString(),dataset.value(4).toString(),
                        dataset.value(5).toString(),dataset.value(6).toString(),
                        dataset.value(7).toString()};

            count++;
        }
    }
    db.dbclose(data);
}

void InfoRecord::update(void){
    beginResetModel();
    getData();
    endResetModel();
}

QVariant InfoRecord::data(const QModelIndex &index, int role) const
{

    if(role == Qt::DisplayRole)
    {
        return QVariant();
    }
    if(index.column() < 0 ||
            columnCount() <= index.column() ||
            index.row() < 0 ||
            rowCount() <= index.row())
    {
        return QVariant();
    }
    switch(role)
    {
    case IdRole:
        return model_data[index.row()].in_id;
    case StateRole:
        return  model_data[index.row()].in_state;
    case BdateRole:
        return  model_data[index.row()].in_bdata;
    case HdateRole:
        return  model_data[index.row()].in_hdata;
    case NameRole:
        return  model_data[index.row()].in_name;
    case InfoRole:
        return  model_data[index.row()].in_info;
    case ValueRole:
        return  model_data[index.row()].in_value;
    case BeizhuRole:
        return  model_data[index.row()].in_beizhu;

    default:
        qDebug() << "Not supposed to happen";
        return QVariant();
    }
}




QHash<int, QByteArray> InfoRecord::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[IdRole] = "bianhao";
    roles[StateRole] = "zhuangtai";
    roles[BdateRole] = "baojingshijian";
    roles[HdateRole] = "querenhuifushijian";
    roles[NameRole] = "mingcheng";
    roles[InfoRole] = "xingxi";
    roles[ValueRole] = "baojingzhi";
    roles[BeizhuRole] = "beizhu";

    return roles;
}
